from flask import *
from flask_sqlalchemy import SQLAlchemy
from flask_wtf import CSRFProtect
from flask_migrate import Migrate,MigrateCommand
from flask_script import Manager
from .config import app_config_dice
from flask_session import Session
from web.info.utils.log import *

db = None

def build_manager(level):
    # 创建app
    app = Flask(__name__)
    # 获得app对应的配置文件,根据开发级别
    app_config = app_config_dice[level]
    # app加载配置文件
    app.config.from_object(app_config)
    # 开启CSRF保护
    CSRFProtect(app)
    # 开启Session代理,写入redis,其配置文件在app_config中
    Session(app)
    # 创建db对象
    global db
    db = SQLAlchemy(app)
    # 生成manager
    manager = Manager(app)
    # 数据库迁移配置
    Migrate(app,db)
    manager.add_command('db',MigrateCommand)
    # 开启log记录
    log_file(app_config.LOGGING_LEVEL)

    from web.info.modules.news import news_blue
    app.register_blueprint(news_blue)

    return manager